package com.mingtu.jgspatrol.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.bean.WeatherBean;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class EcWeatherAdapterNew extends RecyclerView.Adapter<EcWeatherAdapterNew.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private WeatherBean.DataBean mData;
    private OnItemClickListener mOnItemClickListener = null;
    private int[] img1 = {
            R.mipmap.img_weather6, //大气压
            R.mipmap.img_weather5,  //湿度
            R.mipmap.img_weather12, //pm2.5
            R.mipmap.img_weather11, //pm10
            R.mipmap.img_weather2, //风向
            R.mipmap.img_weather4, //温度
            R.mipmap.img_weather3, //雨量
            R.mipmap.img_weather1, //风速
            R.mipmap.img_co,  //一氧化碳
            R.mipmap.img_hydro8,  //空气质量
            R.mipmap.img_no2,  //二氧化氮
            R.mipmap.img_o3, //臭氧
            R.mipmap.img_so2  //二氧化硫
    };

    private int[] img2 = {
            R.mipmap.img_weather6_, R.mipmap.img_weather5_,
            R.mipmap.img_weather12_, R.mipmap.img_weather11_,
            R.mipmap.img_weather2_, R.mipmap.img_weather4_,
            R.mipmap.img_weather3_, R.mipmap.img_weather1_,
            R.mipmap.img_co_, R.mipmap.img_hydro8_,
            R.mipmap.img_no2_, R.mipmap.img_o3_, R.mipmap.img_so2_};


    private boolean isTable = DeviceUtils.isTablet();


    public EcWeatherAdapterNew(Context context, WeatherBean.DataBean mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ec, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view, isTable);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (isTable) {
            holder.ivPicture.setBackgroundResource(img2[position]);
        } else {
            holder.ivPicture.setBackgroundResource(img1[position]);
        }
        if (mData != null) {
            String pressure = mData.getPressure(); //大气压
            String humidity = mData.getHumidity(); //湿度
            String pm2p5 = mData.getPm2p5(); //pm2.5
            String pm10 = mData.getPm10(); //pm10
            String winddir = mData.getWinddir(); //风向
            String temp = mData.getTemp(); //温度
            String precip = mData.getPrecip(); //雨量
            String windspeed = mData.getWindspeed(); //风速
            String co = mData.getCo(); //一氧化碳
            String category = mData.getCategory(); //空气质量
            String no2 = mData.getNo2(); //二氧化氮
            String o3 = mData.getO3(); //臭氧
            String so2 = mData.getSo2(); //二氧化硫
            String pubTime = mData.getPubTime(); //空气数据更新时间
            String obsTime = mData.getObsTime(); //天气数据更新时间

            if (isTable) {
                if (position % 2 == 0) {
                    holder.tvNum.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    holder.tvUnit.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    holder.tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    holder.tvTime.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                } else {
                    holder.tvNum.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    holder.tvUnit.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    holder.tvName.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    holder.tvTime.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                }
            } else {
                if (position % 4 == 0 || (position + 1) % 4 == 0) {
                    holder.tvNum.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    holder.tvUnit.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    holder.tvName.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    holder.tvTime.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                } else {
                    holder.tvNum.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    holder.tvUnit.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    holder.tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    holder.tvTime.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                }
            }

            if (position == 0) {
                holder.tvNum.setText(pressure + "");
                holder.tvUnit.setText("kpa");
                holder.tvName.setText("大气压力");
                holder.tvTime.setText(obsTime);
            } else if (position == 1) {
                holder.tvNum.setText(humidity + "");
                holder.tvUnit.setText("%");
                holder.tvName.setText("大气湿度");
                holder.tvTime.setText(obsTime);
            } else if (position == 2) {
                holder.tvNum.setText(pm2p5 + "");
                holder.tvUnit.setText("ug/m³");
                holder.tvName.setText("PM2.5");
                holder.tvTime.setText(pubTime);
            } else if (position == 3) {
                holder.tvNum.setText(pm10 + "");
                holder.tvUnit.setText("ug/m³");
                holder.tvName.setText("PM10");
                holder.tvTime.setText(pubTime);
            } else if (position == 4) {
                holder.tvNum.setText(winddir);
                holder.tvUnit.setText("");
                holder.tvName.setText("风向");
                holder.tvTime.setText(obsTime);
            } else if (position == 5) {
                holder.tvNum.setText(temp + "");
                holder.tvUnit.setText("°C");
                holder.tvName.setText("大气温度");
                holder.tvTime.setText(obsTime);
            } else if (position == 6) {
                holder.tvNum.setText(precip + "");
                holder.tvUnit.setText("mm");
                holder.tvName.setText("雨量");
                holder.tvTime.setText(obsTime);
            } else if (position == 7) {
                holder.tvNum.setText(windspeed + "");
                holder.tvUnit.setText("m/s");
                holder.tvName.setText("风速");
                holder.tvTime.setText(obsTime);
            } else if (position == 8) {
                holder.tvNum.setText(co);
                holder.tvUnit.setText("µg/m³");
                holder.tvName.setText("一氧化碳");
                holder.tvTime.setText(pubTime);
            } else if (position == 9) {
                holder.tvNum.setText(category);
                holder.tvUnit.setText("");
                holder.tvName.setText("空气质量");
                holder.tvTime.setText(pubTime);
            } else if (position == 10) {
                holder.tvNum.setText(no2);
                holder.tvUnit.setText("µg/m³");
                holder.tvName.setText("二氧化氮");
                holder.tvTime.setText(pubTime);
            } else if (position == 11) {
                holder.tvNum.setText(o3);
                holder.tvUnit.setText("µg/m³");
                holder.tvName.setText("臭氧");
                holder.tvTime.setText(pubTime);
            } else if (position == 12) {
                holder.tvNum.setText(so2);
                holder.tvUnit.setText("µg/m³");
                holder.tvName.setText("二氧化硫");
                holder.tvTime.setText(pubTime);
            }

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return img1.length;
    }

    public void upData(WeatherBean.DataBean mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        public LinearLayout layout;
        public ImageView ivPicture;
        public TextView tvNum;
        public TextView tvUnit;
        public TextView tvName;
        public TextView tvTime;

        public ActivityHolder(View itemView, boolean isTable) {
            super(itemView);
            convertView = itemView;
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            ivPicture = (ImageView) convertView.findViewById(R.id.iv_picture);
            tvNum = (TextView) convertView.findViewById(R.id.tv_num);
            tvUnit = (TextView) convertView.findViewById(R.id.tv_unit);
            tvName = (TextView) convertView.findViewById(R.id.tv_name);
            tvTime = (TextView) convertView.findViewById(R.id.tv_time);

            ViewGroup.LayoutParams lp = layout.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行3个，间隙是20dp

            int mWidth = (screenWidth - SizeUtils.dp2px(20)) / 2;
            int mHeight = mWidth * SizeUtils.dp2px(125) / SizeUtils.dp2px(178);
            if (isTable) {
                mWidth = (screenWidth - SizeUtils.dp2px(26)) / 3;
                mHeight = mWidth * SizeUtils.dp2px(181) / SizeUtils.dp2px(258);
            }

            lp.width = mWidth;
            lp.height = mHeight;
            layout.setLayoutParams(lp);
        }
    }

}
